//
//  LKUserInforTool.m
//  Mideleague_Agent
//
//  Created by Zhiwang on 2020/6/1.
//  Copyright © 2020 Zhiwang. All rights reserved.
//

#import "LKUserInforTool.h"



#define LKAccountPath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"account.archive"]


@implementation LKUserInforTool
+ (void)saveAccount:(LKUserInfor *)account
{
    //1.对需要保存的数据进行编码 ->NSdata *
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:account requiringSecureCoding:YES error:nil];
    //创建文件
    [[NSFileManager defaultManager] createFileAtPath:LKAccountPath contents:nil attributes:nil];
    [data writeToFile:LKAccountPath atomically:YES];
    if ([data writeToFile:LKAccountPath atomically:YES]) {
        NSLog(@"保存成功");
    }else{
        NSLog(@"保存失败");
    }
//    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (LKUserInfor *)account
{

    @try {
        NSData *data = [NSData dataWithContentsOfFile:LKAccountPath];
        return [NSKeyedUnarchiver unarchivedObjectOfClass:[LKUserInfor class] fromData:data error:nil];
    } @catch (NSException *exception) {
        return nil;
    } @finally {

    }
}

+ (void)noAccount
{
    NSFileManager *manager = [NSFileManager defaultManager];
    [manager removeItemAtPath:LKAccountPath error:nil];
}
@end
